/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package invsys.Controladores;


import invsys.Modelo.Variable;
import invsys.Utilidades.ManejadorBaseDatos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author dell
 */
public class ControlVariable {
    private static Connection con;
    private static List variabless;
  public static Variable load (ResultSet rs) throws SQLException{
        Variable v= new Variable();
        v.setCodigo(rs.getString(1));
        v.setDescripcion(rs.getString(2));
        v.setValor(rs.getDouble(3));
        return v;
    }
  public static void registrarVariables(Variable v) throws SQLException, Exception {
        ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
        mbd.conectar();
        con = mbd.getConexion();
        if (con == null) {
            throw new SQLException(" no hay conexion ");
        }
        PreparedStatement pst = null;

        try {

            pst = con.prepareStatement("Insert Into variables values(?,?,?)");
            pst.setString(1, v.getCodigo());
            pst.setString(2, v.getDescripcion());
            pst.setDouble(3, v.getValor());

            pst.executeUpdate();
        } finally {
            if (pst != null) {
                pst.close();
            }
        }
    }

    public static Variable buscarVariables(String ide) throws SQLException {
        if (ide == null) {
            throw new SQLException("No hay elemento clave de la clase novedades");
        }
        ResultSet rs = null;
        PreparedStatement pst = null;
        Variable variable = null;
        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("select * from variables where cod= ?");
            pst.setString(1, ide.trim());

            rs = pst.executeQuery();
            while (rs.next()) {
                variable = load(rs);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            return variable;
        }
    }


     public static void modificarVariables(Variable v, String ide) throws SQLException, Exception {
        if (buscarVariables(ide) == null) {
            throw new SQLException("Variables no registradas ");
        }
        PreparedStatement pst = null;

        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("Update variables set cod = ?,descripcion = ?, valor=? WHERE cod=? ");
            pst.setString(1, v.getCodigo());
            pst.setString(2, v.getDescripcion());
            pst.setDouble(3,v.getValor());
            pst.setString(4,ide);
            pst.executeUpdate();
        } finally {
            if (pst != null) {
                pst.close();
            }
        }

    }

    public static void listarVariables() throws SQLException, Exception {
     ResultSet rs = null;
     PreparedStatement pst = null;
     variabless = new LinkedList();
     try {
         ManejadorBaseDatos mbd=ManejadorBaseDatos.getInstancia();
         mbd.conectar();
         con=mbd.getConexion();
        pst = con.prepareStatement("select * from variables ");
        rs = pst.executeQuery();
        while(rs.next()) {
           variabless.add(load(rs));
        }
    } finally {
        if (rs != null) {
           rs.close();
       }
        if (pst != null) {
          pst.close();
       }
}
}

    public static void eliminarVariables(String ide) throws SQLException, Exception {
        if (buscarVariables(ide) == null) {
            throw new SQLException("No hay Variables clave de la clase novedades");
        }
        PreparedStatement pst = null;

        try {
            ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
            mbd.conectar();
            con = mbd.getConexion();
            pst = con.prepareStatement("delete from variables where cod = ?");
            pst.setString(1, ide.trim());
            pst.executeUpdate();
        } finally {
            if (pst != null) {
                pst.close();
            }
        }
    }

            public static List getVariables() {
        return variabless;
    }

    public static void setVariables(List aVariables) {
        variabless = aVariables;
    }
    
}
